﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace GenUI.WPF.GenControls
{
    internal class THourView : Control
    {
        DataGrid dgHour;
        //ImageButton iBtnClose;
        TextBlock tbTitle;
        public string formerHourStr = string.Empty;
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(THourView), new PropertyMetadata("小   时"));
        static THourView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(THourView), new FrameworkPropertyMetadata(typeof(THourView)));
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            dgHour = GetTemplateChild("PART_dgHour") as DataGrid;
            //iBtnClose = GetTemplateChild("PART_iBtnClose") as ImageButton;
            tbTitle = GetTemplateChild("PART_tbTitle") as TextBlock;
            this.Loaded += (s, e) => LoadHour();
            //iBtnClose.Click += (s, e) => OnHourClickContentEdit(formerHourStr);
            dgHour.SelectedCellsChanged += DgHour_SelectedCellsChanged;
        }
        private void DgHour_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DataGridCellInfo cell = dgHour.CurrentCell;
            if (cell.Column == null)
            {
                return;
            }

            Hour hour = cell.Item as Hour;

            string str1 = string.Empty; ;
            switch (cell.Column.DisplayIndex)// 通过所在列 获取类Hour的坐标 确定具体的hour数据
            {
                case 0:
                    str1 = hour.Hour1.ToString();
                    break;
                case 1:
                    str1 = hour.Hour2.ToString();
                    break;
                case 2:
                    str1 = hour.Hour3.ToString();
                    break;
                case 3:
                    str1 = hour.Hour4.ToString();
                    break;
                case 4:
                    str1 = hour.Hour5.ToString();
                    break;
                case 5:
                    str1 = hour.Hour6.ToString();
                    break;

                default: break;
            }

            str1 = str1.PadLeft(2, '0');
            OnHourClickContentEdit(str1);
        }
        public Action<string> HourClick;
        protected void OnHourClickContentEdit(string hourstr)
        {
            HourClick?.Invoke(hourstr);
        }

        #region  类

        public class Hour
        {
            public int Hour1 { get; set; }
            public int Hour2 { get; set; }
            public int Hour3 { get; set; }
            public int Hour4 { get; set; }
            public int Hour5 { get; set; }
            public int Hour6 { get; set; }
            public Hour(int hour1, int hour2, int hour3, int hour4, int hour5, int hour6)
            {
                Hour1 = hour1;
                Hour2 = hour2;
                Hour3 = hour3;
                Hour4 = hour4;
                Hour5 = hour5;
                Hour6 = hour6;

            }

        }
        #endregion    

        #region 方法
        /// <summary>
        /// dgHour控件 绑定类Hour 加载初始化数据
        /// </summary>
        public void LoadHour()
        {
            Hour[] hour = new Hour[4];

            hour[0] = new Hour(0, 1, 2, 3, 4, 5);
            hour[1] = new Hour(6, 7, 8, 9, 10, 11);
            hour[2] = new Hour(12, 13, 14, 15, 16, 17);
            hour[3] = new Hour(18, 19, 20, 21, 22, 23);

            dgHour.Items.Clear();
            dgHour.ItemsSource = hour;
        }

        #endregion
    }
}
